package com.isoftstone.project.timedtask;

import org.springframework.beans.BeansException;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
 import org.springframework.stereotype.Component;
  @Component
public class SpringBeanUtil implements ApplicationContextAware {
 private static ApplicationContext applicationContext = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws
                 BeansException {
              SpringBeanUtil.applicationContext = applicationContext;
        }

             /**
 18      * 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
 19      */
             @SuppressWarnings("unchecked")
 public static <T> T getBean(String name) {
                if(name == null || applicationContext == null)
                       return null;
                return (T) applicationContext.getBean(name);
          }



     public static <T> T getBean(Class<T> clazz) {
         return applicationContext.getBean(clazz);
     }

      }